﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Watson.TemporaryFilesDeleter
{
    /// <summary>
    /// Provides a method to filter a collection of files by the last modified
    /// date.
    /// </summary>
    public class LastModifiedFileFilter : FileFilter
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the earliest value for the modified date of a file
        /// which will result in the file passing the filter and being included
        /// in the output collection.
        /// </summary>
        public DateTime MinimumLastModifiedDate { get; set; }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Determines whether to include a specific file from the input
        /// collection in the output collection.
        /// </summary>
        /// <param name="file">
        /// The file for which to determine its inclusion in the output
        /// collection.
        /// </param>
        /// <returns>
        /// System.Bool
        /// <para>
        /// True if the file was last modified on or after the 
        /// <see cref="MinimumLastModifiedDate"/> and thus should be included in
        /// the output collection; else, false.
        /// </para>
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="file"/> was null.
        /// </exception>
        protected override bool IncludeFile(System.IO.FileInfo file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }


            if (file.LastWriteTime >= this.MinimumLastModifiedDate)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Obtains a string representation of the current 
        /// LastModifiedFileFilter.
        /// </summary>
        /// <returns>
        /// System.String
        /// <para>
        /// String representing the current LastModifiedFileFilter.
        /// </para>
        /// </returns>
        public override string ToString()
        {
            string format = 
                "MinimumModifiedDate: {{{0}}}";

            string value = 
                String.Format(CultureInfo.CurrentCulture,
                              format,
                              this.MinimumLastModifiedDate);

            return value;
        }

        #endregion

        #region Public Constructors

        /// <summary>
        /// Initialises a new instance of the LastModifiedFileFilter class.
        /// </summary>
        public LastModifiedFileFilter()
        { }

        /// <summary>
        /// Initialises a new instance of the LastModifiedFileFilter class
        /// specifying a value for the minimum last modified date.
        /// </summary>
        public LastModifiedFileFilter(DateTime minimumLastModifiedDate)
        {
            this.MinimumLastModifiedDate = minimumLastModifiedDate;
        }

        #endregion
    }
}
